package com.example.client;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.io.*;
import java.sql.Date;
import java.sql.Time;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import static com.example.client.ClientLog.clientDataList2;
import static com.example.server.ServerLog.clientDataList;

public class ClientController {
    @FXML
    private TextField dateText;
    @FXML
    private TextField timeText;
    @FXML
    private TextField temperatureText;
    @FXML
    private TextField humidityText;
    @FXML
    private TextField co2Text;
    @FXML
    private TextField luxText;
    @FXML
    public TextField addressText;
    @FXML
    private Button sendButton;
    @FXML
    private Button myDataButton;
    @FXML
    private Button startButton;


    String id;
    java.util.Date allDate;
    Date date;
    Time time;
    int port;
    String address;
    Boolean f=true;

    //数据随机数
    Random r = new Random();
    String temperature1;
    double temperature;
    String humidity1;
    double humidity;
    int co2;
    int lux;
    java.text.DecimalFormat df = new DecimalFormat("#.##");
    Timer timer = new Timer();//定时器

    ClientLog clientLog;

    @FXML
    public void initialize(){
        //refresh();
        id = String.valueOf(System.currentTimeMillis());//由时间生成的客户端id

    }

    @FXML
    public void onStartButtonAction(ActionEvent actionEvent) { //方法被覆盖掉了

        port = 8888;
        address = addressText.getText();//获取位置信息
        //启动Client线程
        new Thread(new MyClient(dateText,timeText,temperatureText,humidityText,co2Text,luxText, addressText,sendButton)).start();
        //获取随机数据并打印
        System.out.println("点击按钮");

        if(f)
        {
            startButton.setText("停止监测");
            f=false;

            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            ClientData clientData =  refresh();
                            clientDataList.add(clientData);
                            clientDataList2.add(clientData);

                            //将产生的数据写入文件
                            String word ="date:" + dateText.getText() + " time:" + timeText.getText() + " temperature:" + temperatureText.getText() + " humidity:" + humidityText.getText() + " co2:" + co2Text.getText() + " lux:" + luxText.getText();
                            String path = "src/main/resources/com/example/copyClientData/" + address;
                            BufferedWriter out = null;
                            try {
                                out = new BufferedWriter(
                                        new OutputStreamWriter(new FileOutputStream(path,true)));
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            try {
                                out.write(word+"\r\n");
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            try {
                                out.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    });
                }
            },100,2000);
        }
        else {
            startButton.setText("开始监测");
            f=true;
            timer.cancel();
        }

    }

    public ClientData refresh(){
        ClientData clientData = new ClientData();
        //获取随机数据并打印
        allDate = new java.util.Date();
        //日期随机数
        date = new Date(allDate.getTime());
        //时间随机数
        time = new Time(allDate.getTime());
        //温度随机数
        temperature1 = df.format(r.nextDouble()*71-20) ;  //-20℃~50℃
        temperature = Double.parseDouble(temperature1);
        //湿度随机数
        humidity1 = df.format(r.nextDouble()*100); //0~100%RH
        humidity = Double.parseDouble(humidity1);
        //co2随机数
        co2 = r.nextInt(2000); //0~2000ppm
        //光照强度随机数
        lux = r.nextInt(500); //0~500lx

        dateText.setText(date.toString());
        timeText.setText(time.toString());
        temperatureText.setText(String.valueOf(temperature));
        humidityText.setText(String.valueOf(humidity));
        co2Text.setText(String.valueOf(co2));
        luxText.setText(String.valueOf(lux));

        clientData.setDate(date.toString());
        clientData.setTime(time.toString());
        clientData.setTemperature(String.valueOf(temperature));
        clientData.setHumidity(String.valueOf(humidity));
        clientData.setCo2(String.valueOf(co2));
        clientData.setLux(String.valueOf(lux));

        return clientData;
    }

    public void onMyDataButton(ActionEvent actionEvent) throws IOException {//弹出用户数据页面
        //弹出新的页面
        ClientLog clientLog = new ClientLog();
        clientLog.showMyData();

    }
}
